package database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MySQLProduktBatchKompDAO implements ProduktBatchKompDAO {

	private Connector conn = null;
	private ResultSet rs = null;

	public MySQLProduktBatchKompDAO() throws DALException {
		conn = new Connector();
	}

	public ProduktBatchKompDTO getProduktBatchKomp(int pbId, int rbId)
			throws DALException {
		ProduktBatchKompDTO pbk = null;

		String query = "SELECT * FROM produktbatchkomp WHERE pbId = " + pbId
				+ " AND rbId = " + rbId + ";";
		rs = conn.doQuery(query);
		try {
			if (!rs.first()) {
				throw new DALException(
						"ProduktBatchKomponent med den angivne produktbatchId "
								+ pbId + "findes ikke");
			}
			pbk = new ProduktBatchKompDTO(rs.getInt(1), rs.getInt(2),
					rs.getDouble(3), rs.getDouble(4), rs.getInt(5));
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return pbk;

	}

	public List<ProduktBatchKompDTO> getProduktBatchKompList(int pbId) throws DALException {
		List<ProduktBatchKompDTO> pbk = new ArrayList<ProduktBatchKompDTO>();
		String query = "SELECT *FROM produktbatchkomp WHERE pbId = " + pbId
				+ ";";
		rs = conn.doQuery(query);
		try {
			while (rs.next()) {
				pbk.add(new ProduktBatchKompDTO(rs.getInt(1), rs.getInt(2), rs
						.getDouble(3), rs.getDouble(4), rs.getInt(5)));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

		return pbk;

	}

	public ArrayList<ProduktBatchKompDTO> getProduktBatchKompList() throws DALException {
		ArrayList<ProduktBatchKompDTO> pbkList = new ArrayList<ProduktBatchKompDTO>();
		String query = "SELECT *FROM produktbatchkomp";
		rs = conn.doQuery(query);
		try {
			while (rs.next()) {
				pbkList.add(new ProduktBatchKompDTO(rs.getInt(1), rs.getInt(2),
						rs.getDouble(3), rs.getDouble(4), rs.getInt(5)));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pbkList;
	}

	public List<ProduktBatchKompDTO> getProduktBatchKompList(int first, int count) throws DALException {

		List<ProduktBatchKompDTO> pbkList = new ArrayList<ProduktBatchKompDTO>();
		String query = "SELECT * FROM produktbatchkomp LIMIT " + first + ","
				+ count;
		rs = conn.doQuery(query);
		try {
			while (rs.next()) {
				pbkList.add(new ProduktBatchKompDTO(rs.getInt(1), rs.getInt(2),
						rs.getInt(3), rs.getInt(4), rs.getInt(5)));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pbkList;
	}

	public void createProduktBatchKomp(ProduktBatchKompDTO produktbatchkomponent) throws DALException {
		conn.doUpdate("INSERT INTO produktbatchkomp (rbId, tara, netto, oprId) VALUES"
				+ "("
				+ produktbatchkomponent.getRbId()
				+ ","
				+ produktbatchkomponent.getTara()
				+ ","
				+ produktbatchkomponent.getNetto()
				+ ","
				+ produktbatchkomponent.getOprId() + ");");

	}

	public void updateProduktBatchKomp(ProduktBatchKompDTO produktbatchkomponent) throws DALException {
		conn.doUpdate("UPDATE produktbatchkomp SET pbId = "
				+ produktbatchkomponent.getPbId() + ", rbId =  "
				+ produktbatchkomponent.getRbId() + ", tara = "
				+ produktbatchkomponent.getTara() + ", netto = "
				+ produktbatchkomponent.getNetto() + ", oprId = "

				+ produktbatchkomponent.getOprId() + " WHERE pbId = "
				+ produktbatchkomponent.getPbId() + " AND rbId = "
				+ produktbatchkomponent.getRbId());

	}

	public int getCount() throws DALException {
		rs = conn.doQuery("SELECT COUNT(pbId) FROM produktbatchkomp");
		try {
			if (rs.next())
				return rs.getInt(1);
			else
				return 0;
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
}
